<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>备件需求计划审批详情</title>
    <link rel="stylesheet" href="/erms/statics/plugins/layui-v2.6.8/css/layui.css">
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>江北发动机厂车间月度备件需求计划审批</legend>
</fieldset>
<form class="layui-form" lay-filter="formTest">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">所属车间：</label>
            <div class="layui-input-inline">
                <input type="text" name="workshopName" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">编制月份：</label>
            <div class="layui-input-inline">
                <input type="text" name="organMonth" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">编制单位：</label>
            <div class="layui-input-inline">
                <input type="text" name="deptName" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">编制人：</label>
            <div class="layui-input-inline">
                <input type="text" name="organName" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="export">导出附件</button>
        </div>
    </div>

    <!-- 领用备件申请列表 -->
    <table id="demo" lay-filter="test"></table>
    <!-- 备件领用审批记录 -->
    <table id="auditHistory" lay-filter="auditHistory"></table>
    <div class="layui-form-item" id="auditRadio">
        <label class="layui-form-label">审批</label>
        <div class="layui-input-block">
            <input type="radio" name="state" value="1" title="同意" checked>
            <input type="radio" name="state" value="2" title="不同意">
        </div>
    </div>
    <div class="layui-form-item layui-form-text" id="auditTextArea">
        <label class="layui-form-label">审批意见</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入审批意见" name="checkRemark" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item" id="auditButton">
        <div class="layui-input-block">
            <input type="hidden" name="id"/>
            <input type="hidden" name="workItemId"/>
            <button class="layui-btn" lay-submit lay-filter="confirmAudit">确认审核</button>
        </div>
    </div>
</form>
<script type="text/html" id="operation">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="/erms/statics/plugins/layui-v2.6.8/layui.js"></script>
<script type="text/javascript" src="/erms/statics/plugins/jquery.min.js"></script>
<script>
    //等待计时 是否等待
    var awitTime = true;
    layui.use(['layer', 'table', 'form'], function () {
        var layer = layui.layer, form = layui.form, table = layui.table;
        var procInstId = getQueryParam('procInstId');
        var workItemId = getQueryParam('workItemId');
        if(!procInstId && !workItemId){
            // 审核区域全部不显示
            hideAuditArea();
            // 理论上不会发生流程ID和工作项ID都没有传递的场景
            layer.msg('当前页只用于OA系统工厂车间月度工装备件计划审核，请确认！', { icon: 7, time: 5000, shade: [0.6, '#000', true] });
        }else{
            // 拼接查询参数
            var queryParam = '';
            // 如果流程ID存在，则拼接查询参数
            if(procInstId){
                queryParam = 'procInstId=' + procInstId;
            }
            // 如果OA审批项ID存在，则拼接查询参数
            if(workItemId){
                if(queryParam){
                    queryParam += '&workItemId=' + workItemId;
                }else{
                    queryParam = 'workItemId=' + workItemId;
                }
            }else{
                // 工作项ID不存在，也隐藏审核区域
                hideAuditArea();
                workItemId = '';
            }
            var getUrl = '/erms/sparepartrequ/loadSpNeedsPlanDetail';
            if(queryParam){
                getUrl += '?' + queryParam;
            }
            // 拉取服务器端
            $.get(getUrl, function (data, status) {
                if (status == 'success' && data.code == 0) {
                    //给表单赋值
                    form.val("formTest", {
                        "workshopName": data.detail.workshopName,
                        "organMonth": data.detail.organMonth,
                        "deptName": data.detail.deptName,
                        "organName": data.detail.organName,
                        "id": data.detail.id,
                        "workItemId": workItemId
                    });
                    // 渲染备件列表
                    table.render({
                        elem: '#demo',
                        data: data.items,//json数据
                        cols: [[
                            { colspan: 20, title: '领料申请列表', align: "center"}
                        ],[
                            {field: 'id', hide: true, title: '主键'},
                            {type: 'numbers', title:'序号', width: '3%'},
                            {field: 'organUserName', width: '5%', align: "center", title: '提出人'},
                            {field: 'materialCode', width: '8%', align: "center", title: '备件编码'},
                            {field: 'erpCode', width: '8%', align: "center", title: 'ERP编码'},
                            {field: 'materialName', width: '10%', align: "center", title: '备件名称'},
                            {field: 'spec', width: '10%', align: "center", title: '规格型号'},
                            {field: 'unit', width: '4%', align: "center", title: '单位'},
                            {field: 'typeDesc', width: '6%', align: "center", title: '备件类型'},
                            {field: 'installPosition', width: '10%', align: "center", title: '备件类型'},
                            {field: 'planAbc', width: '4%', align: "center", title: 'ABC类'},
                            {field: 'stockNum', width: '4%', align: "center", title: '库存'},
                            {field: 'demandNum', width: '4%', align: "center", title: '需求'},
                            {field: 'transitNum', width: '4%', align: "center", title: '在途'},
                            {field: 'producor', width: '10%', align: "center", title: '制造商'},
                            {field: 'planPrice', width: '5%', align: "center", title: '单价'},
                            {field: 'money', width: '5%', align: "center", title: '金额'}
                        ]],
                        limit: data.items.length,
                        page: false
                    });
                    if (data.auditRecordList.length > 0) {
                        // 渲染审批历史
                        table.render({
                            elem: '#auditHistory',
                            data: data.auditRecordList,//json数据
                            cols: [[
                                { colspan: 7, title: '领料审核进度', align: "center"}
                            ],[
                                {field: 'id', hide: true, title: '主键'},
                                {type: 'numbers', title:'序号', width: '5%'},
                                {field: 'workitemName', width: '25%', align: "center", title: '环节名称'},
                                {field: 'approverName', width: '15%', align: "center", title: '处理人'},
                                {field: 'state', width: '15%', align: "center", title: '审批结果',templet: function(d){
                                        return d.state == 1?'同意':'不同意';
                                    }},
                                {field: 'remark', width: '25%', align: "center", title: '审批意见'},
                                {field: 'approveDate', width: '15%', align: "center", title: '处理时间'}
                            ]],
                            limit: data.auditRecordList.length,
                            page: false
                        });
                    }
                }
            });
        }
        form.on('submit(confirmAudit)', function (data) {
            /*
           1.全局变量 awitTime 为true 点击确认（btn1）了之后 为false ，①确定5s后为true ②请求成功为true
           * */
            if(awitTime){
                layer.confirm('您确认提交审核记录吗？',{
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        awitTime = false;
                        setTimeout(function(){
                            awitTime = true;
                        },5000)
                        // 先关闭弹窗
                        layer.close(index);
                        // 提交审核数据并跳转
                        $.post('/erms/sparepartrequ/check',data.field,function (res){
                            awitTime = true;
                            // 跳转到后续流程页
                            window.open(res)
                        });
                    },
                    btn2: function (index) {
                        // 先关闭弹窗
                        layer.close(index);
                    }
                });
            }else{
                layer.msg('请求未结束，请等待！', { icon: 7});
            }
            return false;
        });
        form.on('submit(export)', function (data) {
            window.open('../sparepartrequ/export?planIds=' + data.field.id);
            return false;
        });
    });
    function getQueryParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    }
    // 隐藏审核区域
    function hideAuditArea(){
        // 审核区域全部不显示
        $('#auditRadio').hide();
        $('#auditTextArea').hide();
        $('#auditButton').hide();
    }
</script>
</body>
</html>
